package com.zhuzhiguang.student.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhuzhiguang.student.dao.StudentDao;
import com.zhuzhiguang.student.entity.Favourite;
import com.zhuzhiguang.student.entity.Student;
import com.zhuzhiguang.student.service.StudentService;

@Service
public class StudentServiceImpl implements StudentService {

	@Autowired
	StudentDao stuDao;
	
	@Override
	public List<Student> list(int page, int pageSize) {
		// TODO Auto-generated method stub
		return stuDao.list();
	}

	@Override
	public void add(Student stu) {
		// TODO Auto-generated method stub
		//添加主表
		stuDao.addStu(stu);
		// 添加中间表
		for (Integer  favId : stu.getFavIds()) {
			stuDao.addStuFav(stu.getId(), favId);
		}
		
	}

	@Override
	public void delete(int... ids) {
		// TODO Auto-generated method stub
		//删除中间表
		stuDao.deleteStuFav(ids);
		// 删除主表
		stuDao.deleteStu(ids);
		
	}

	@Override
	public void update(Student stu) {
		// TODO Auto-generated method stub
		//修改主表内容
		stuDao.update(stu);
		//删除中间表
		stuDao.deleteStuFav(stu.getId());
		//重新添加中间表
		for (Integer  favId : stu.getFavIds()) {
			stuDao.addStuFav(stu.getId(), favId);
		}
	}

	@Override
	public List<Favourite> listFavs() {
		// TODO Auto-generated method stub
		return stuDao.listFavs();
	}

	@Override
	public Student getById(int id) {
		// TODO Auto-generated method stub
		return stuDao.findById(id);
	}

}
